package com.foreknow.elm.service.Impl;

import com.foreknow.elm.dao.Impl.OrderDaoImpl;
import com.foreknow.elm.dao.Impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.OrderDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrderService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    @Override
    public int createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        int OrderId=0;
        List<Cart>li=new ArrayList<>();
        List<Orderdetailet> list=new ArrayList<>();
        OrderDao orderDao=new OrderDaoImpl();
        OrderdetailetDao orderdetailetDao=new OrderdetailetDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            OrderId=orderDao.OrdersInsert(userId,businessId,daId,orderTotal);
            li=orderDao.OrdersSelect(userId,businessId);
            for (int i=0;i<li.size();i++){
                Orderdetailet od=new Orderdetailet();
                od.setOrderId(OrderId);
                od.setFoodId(li.get(i).getFoodId());
                od.setQuantity(li.get(i).getQuantity());
                list.add(od);
            }

//            if (li!=null){
//                System.out.println("查询成功");
//            }else {
//                System.out.println("查询失败");
//            }
            int a=orderdetailetDao.OrdersDeInsert(list);
//            if (a!=0){
//                System.out.println("插入成功");
//            }else {
//                System.out.println("插入失败");
//            }
            int b=orderDao.CartRemove(userId,businessId);
//            if (b!=0){
//                System.out.println("删除成功");
//            }else {
//                System.out.println("删除失败");
//            }
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return OrderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        OrderDao orderDao=new OrderDaoImpl();
        OrderdetailetDao odd=new OrderdetailetDaoImpl();
        Orders orders=new Orders();
        try {
            DBUtil.beginTransaction("false");
            orders=orderDao.getOrdersById(orderId);
            List<Orderdetailet> list=odd.getOD(orderId);
            orders.setList(list);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders>list=new ArrayList<>();
        OrderdetailetDao odd=new OrderdetailetDaoImpl();
        OrderDao orderDao=new OrderDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            list=orderDao.listOrdersByUserId(userId);
            for (int i=0;i< list.size();i++){
                list.get(i).setList(odd.getOD(list.get(i).getOrderId()));
            }
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }

    public static void main(String[] args) {
        OrderService os=new OrderServiceImpl();

//        Orders o=os.getOrdersById(24);
//        System.out.println(o.getBusinessId()+"---"+o.getOrderId()+"---"+o.getBusiness().getBusinessId()+"---"+o.getBusiness().getBusinessName()+"---"+o.getList().size());
//        for (int i=0; i<o.getList().size();i++){
//            System.out.println(o.getList().get(i).getOdId()+"--"+o.getList().get(i).getFood().getFoodName());
//        }

//        List<Orders>list=os.listOrdersByUserId("11111111111");
//        for (int i=0;i< list.size();i++){
//            System.out.println(list.get(i).getOrderId()+"---"+list.get(i).getList().size());
//            for (int j=0;j<list.get(i).getList().size();j++){
//                System.out.println(list.get(i).getList().get(j).getOrderId()+"---"+list.get(i).getList().get(j).getOdId()+"---"+list.get(i).getList().get(j).getFood().getFoodName());
//            }
//        }
//          System.out.println(os.listOrdersByUserId("12345671111").size());
//        System.out.println(os.createOrders("3333333333",10004,5,203.67));
//        Orders orders= os.getOrdersById(15);
//        System.out.println(orders.getUserId()+"---"+orders.getList().size());
    }
}
